/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2016-11-23
 * 修改历史 :
 * 1. [2016-11-23]创建文件 by chenxin
 */
package com.sinosoft.sinopay.service.payrules.biz.service.impl;

import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.exception.biz.IllegalRequestException;
import com.sinosoft.sinopay.payrules.config.ENUM_IP_BIZTYPE;
import com.sinosoft.sinopay.payrules.config.ENUM_IP_SECURITYLIST;
import com.sinosoft.sinopay.service.payrules.biz.integration.dao.SecurityMapper;
import com.sinosoft.sinopay.service.payrules.biz.service.VisitSecurityCoreService;
import com.sinosoft.sinopay.service.payrules.biz.util.IPUtil;
import com.sinosoft.sinopay.service.payrules.config.ENUM_IP_TYPE;
import com.sinosoft.sinopay.service.payrules.dto.SecurityIpInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 【访问安全服务接口实习那类】
 * @author chenxin
 * @date 2016-11-23 上午11:17 
 * @version V1.0
 */
@Service
public class VisitSecurityCoreServiceImpl extends SinoBaseService implements VisitSecurityCoreService {
    @Autowired
    private SecurityMapper securityMapper;

    private Boolean  initFlag = false;

    private List<String> ipGatewayBlackList = new ArrayList<String>();

    private List<String> ipGatewayWhiteList= new ArrayList<String>();

    private List<String> ipGatewayWhiteAreaList= new ArrayList<String>();

    private List<String> ipGatewayBlackAreaList= new ArrayList<String>();

    @Override
    public void checkIpValid(String ip, ENUM_IP_BIZTYPE cIpBizType, ENUM_IP_SECURITYLIST cSecurityListType) throws IllegalRequestException {
        String tErrorMsg = "ip不合法,ip地址为:" + ip ;
        if(!initFlag){
            initIPSecurityList(cIpBizType);
        }
        if(cSecurityListType == ENUM_IP_SECURITYLIST.BLACK){
            if(!CollectionUtils.isEmpty(ipGatewayBlackList) && ipGatewayBlackList.contains(ip)){
                throw new IllegalRequestException(tErrorMsg);
            }
            if(!CollectionUtils.isEmpty(ipGatewayBlackAreaList)) {
                for (String ipArea : ipGatewayBlackAreaList) {
                    if (IPUtil.ipExistsInRange(ip, ipArea)) {
                        throw new IllegalRequestException(tErrorMsg);
                    }
                }
            }
        }
        else if(cSecurityListType == ENUM_IP_SECURITYLIST.BLACK){
            if(!CollectionUtils.isEmpty(ipGatewayBlackList)){
                if(ipGatewayWhiteList.contains(ip)){
                    return;
                }
            }
            if(!CollectionUtils.isEmpty(ipGatewayWhiteAreaList)) {
                for (String ipArea : ipGatewayWhiteAreaList) {
                    if (IPUtil.ipExistsInRange(ip, ipArea)) {
                        return;
                    }
                }
            }
            throw new IllegalRequestException(tErrorMsg);
        }

    }

    /**
     * 初始化安全ip列表
     * @param cIpBizType
     */
    private void initIPSecurityList(ENUM_IP_BIZTYPE cIpBizType) {
        List<SecurityIpInfo> tIpList = securityMapper.selectIpListByBizType(cIpBizType.code());
        for(SecurityIpInfo tIpInfo:tIpList){
            String ipType = tIpInfo.getIpType();
            String listType = tIpInfo.getListType();
            String ipInfo  = tIpInfo.getIpInfo();
            if(ENUM_IP_TYPE.FIXED.code().equals(ipType)){
                if(ENUM_IP_SECURITYLIST.BLACK.code().equals(listType)){
                    ipGatewayBlackList.add(ipInfo);
                }
                if(ENUM_IP_SECURITYLIST.WHITE.code().equals(listType)){
                    ipGatewayWhiteList.add(ipInfo);
                }
            }else if(ENUM_IP_TYPE.AREA.code().equals(ipType)){
                if(ENUM_IP_SECURITYLIST.BLACK.code().equals(listType)){
                    ipGatewayBlackAreaList.add(ipInfo);
                }
                if(ENUM_IP_SECURITYLIST.WHITE.code().equals(listType)){
                    ipGatewayWhiteAreaList.add(ipInfo);
                }
            }
        }
        initFlag = true;
    }
}
